package com.tl.mqtt.utils;

import org.apache.commons.lang3.StringUtils;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件处理工具类
 * 
 * @author ruoyi
 */
public class FileUtils {


	/**
	 * 文件Base64拆包
	 * @param fileBase64Str 文件base64字符串
	 * @param packSize 子包大小
	 * @return 拆包后列表
	 */
	public static List<String> strUnPacking(String fileBase64Str,int packSize){
		byte[] bytes = ByteUtil.strToBytes(fileBase64Str);
		List<String> list = new ArrayList<>();
		for (int i = 0; i < bytes.length/packSize+1; i++) {
			if(i==bytes.length/packSize){
				int y = bytes.length%packSize;
				byte[] bytes1 = new byte[y];
				for (int j = 0+packSize*i; j < packSize*i+y && j < bytes.length; j++) {
					bytes1[j-(packSize*i)] = bytes[j];
				}
				list.add(ByteUtil.bytesToStr(bytes1)); //模拟拆包存储字符串
			}else {
				byte[] bytes1 = new byte[packSize];
				for (int j = 0+packSize*i; j < packSize*(i+1) && j < bytes.length; j++) {
					bytes1[j-(packSize*i)] = bytes[j];
				}
				list.add(ByteUtil.bytesToStr(bytes1)); //模拟拆包存储字符串
			}
		}
		return list;
	}

	/**
	 * 文件对象
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static byte[] file2byte(File file) throws IOException {
		byte[] data = null;
		FileImageInputStream input = new FileImageInputStream(file);
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];
		int numBytesRead = 0;
		while ((numBytesRead = input.read(buf)) != -1) {
			output.write(buf, 0, numBytesRead);
		}
		data = output.toByteArray();
		output.close();
		input.close();
		return data;
	}

	/**
	 * 文件路径
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static byte[] file2byte(String path) throws IOException {
		byte[] data = null;
		FileInputStream input = new FileInputStream(new File(path));
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];
		int numBytesRead = 0;
		while ((numBytesRead = input.read(buf)) != -1) {
			output.write(buf, 0, numBytesRead);
		}
		data = output.toByteArray();
		output.close();
		input.close();
		return data;
	}

	/**
	 * byte数组到文件
	 * @param data
	 * @param path
	 * @param extension
	 * @return
	 * @throws IOException
	 */
	public static String byte2File(byte[] data, String path, String extension) throws IOException {
		createFileCatalog(path.substring(0, path.lastIndexOf("/")));
		FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
		imageOutput.write(data, 0, data.length);
		imageOutput.close();
		return path;
	}

	/**
	 * 创建文件目录
	 * @param filePath
	 */
	public static void createFileCatalog(String filePath) {
		if (StringUtils.isNotBlank(filePath)) {
			File fileCatalog = new File(filePath);
			if (!fileCatalog.exists()) {
				fileCatalog.mkdirs();
			}
		}
	}
	
	/**
	 * 获取文件扩展名
	 * @param path
	 * @return
	 */
	public static String getExtension(String path) {
		return path.substring(path.lastIndexOf("."));
	}
}
